package src.Players;

import java.io.*;
import src.Util.*;
import src.Main;
import src.GameEngine;

public class PlayerItems
{
    private GameEngine ge;

    public PlayerItems()
    {
        ge = Main.m.ge;
    }

    public boolean addItem(Player p, int itemId, int itemAmt)
    {
        if(itemId <= -1 || itemAmt <= 0 || p == null)
        {
            return false;
        }
        if(!ge.item.stackable(itemId) && !ge.item.noted(itemId))
        {
            for(int i = 0; i < p.items.length; i++)
            {
                int index = findInvSlot(p);
                if(index >= 0 && itemAmt != 0)
                {
                    itemAmt--;
                    p.items[index] = (itemId + 1);
                    p.itemsN[index] = 1;
                }
                else
                {
                    break;
                }
            }
            Main.m.ge.frame.resetItems(p, 3214);
            if(itemAmt == 0)
            {
                return true;
            }
            else
            {
                p.frame.sendMessage(p, "Not enough space in your inventory.");
                return false;
            }
        }
        else if(ge.item.stackable(itemId) || ge.item.noted(itemId))
        {
            if(invItemCount(p, itemId) > 0)
            {
                if((itemAmt + invItemCount(p, itemId)) <= ge.item.maxItemAmount)
                {
                    p.itemsN[getItemSlot(p, itemId)] += itemAmt;
                    Main.m.ge.frame.resetItems(p, 3214);
                    return true;
                }
                else
                {
                    p.itemsN[getItemSlot(p, itemId)] = ge.item.maxItemAmount;
                    Main.m.ge.frame.resetItems(p, 3214);
                    return true;
                }
            }
            else if(invItemCount(p, itemId) <= 0)
            {
                int index = findInvSlot(p);
                if(index >= 0)
                {
                    p.items[index] = (itemId + 1);
                    p.itemsN[index] = itemAmt;
                    Main.m.ge.frame.resetItems(p, 3214);
                    return true;
                }
                else
                {
                    p.frame.sendMessage(p, "Not enough space in your inventory.");
                    return false;
                }
            }
        }
        p.frame.sendMessage(p, "Not enough space in your inventory.");
        return false;
    }

    public boolean deleteItem(Player p, int itemId, int index, int amount)
    {
        if(itemId <= -1 || amount <= 0 || p == null || index < 0 || index > 27)
        {
            return false;
        }
        if((p.items[index] == (itemId + 1)) && (!ge.item.stackable(itemId) && !ge.item.noted(itemId)))
        {
            if(amount == 1 && p.items[index] == (itemId - 1))
            {
                p.items[index] = 0;
                p.itemsN[index] = 0;
                p.frame.resetItems(p, 3214);
                return true;
            }
            else
            {
                for(int i = 0; i < p.items.length; i++)
                {
                    if(p.items[i] == (itemId + 1) && amount != 0)
                    {
                        amount--;
                        p.items[i] = 0;
                        p.itemsN[i] = 0;
                    }
                }
                p.frame.resetItems(p, 3214);
                if(amount == 0)
                {
                    return true;
                }
            }
        }
        if((p.items[index] == (itemId + 1)) && (ge.item.stackable(itemId) || ge.item.noted(itemId)))
        {
            if(p.itemsN[index] > amount)
            {
                p.itemsN[index] -= amount;
                p.frame.resetItems(p, 3214);
                return true;
            }
            else
            {
                p.items[index] = 0;
                p.itemsN[index] = 0;
                p.frame.resetItems(p, 3214);
                return true;
            }
        }
        return false;
    }

    public int getItemSlot(Player p, int itemId)
    {
        if(p == null)
        {
            return 0;
        }
        int itemAmt = 0;
        for(int i = 0; i < p.items.length; i++)
        {
            if(p.items[i] == (itemId + 1))
            {
                return i;
            }
        }
        return -1;
    }

    public int findInvSlot(Player p)
    {
        if(p == null)
        {
            return -1;
        }
        for(int i = 0; i < p.items.length; i++)
        {
            if(p.items[i] <= 0)
            {
                return i;
            }
        }
        return -1;
    }

    public int invItemCount(Player p, int itemId)
    {
        if(p == null)
        {
            return 0;
        }
        int itemAmt = 0;
        for(int i = 0; i < p.items.length; i++)
        {
            if(p.items[i] == (itemId + 1))
            {
                if(p.items[i] == (itemId + 1) && (ge.item.stackable(itemId) || ge.item.noted(itemId)))
                {
                    itemAmt += p.itemsN[i];
                }
                else
                {
                    itemAmt++;
                }
            }
        }
        return itemAmt;
    }
    public int[] playerTItems = new int[28];
    public int[] playerTItemsN = new int[28];
}